﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Utils.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   The utils.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.DataAccess
{
    #region Imports

    using System;
    using System.Web;

    #endregion

    /// <summary>
    /// The utils.
    /// </summary>
    public class Utils
    {
        #region [rgn] Methods (2)

        /// <summary>
        /// Verifies the object argument.
        /// </summary>
        /// <param name="objectToValidate">
        /// The object to validate.
        /// </param>
        /// <param name="argumentName">
        /// Name of the argument.
        /// </param>
        public static void VerifyObjectArgument(object objectToValidate, string argumentName)
        {
            if (objectToValidate == null)
            {
                // Uses reflection, but hey, this is an exception.
                throw new ArgumentNullException(argumentName);
            }
        }

        /// <summary>
        /// Verifies the string argument.
        /// </summary>
        /// <param name="stringToValidate">
        /// The string to validate.
        /// </param>
        /// <param name="argumentName">
        /// Name of the argument.
        /// </param>
        public static void VerifyStringArgument(string stringToValidate, string argumentName)
        {
            if (string.IsNullOrEmpty(stringToValidate))
            {
                // Uses reflection, but hey, this is an exception.
                throw new ArgumentException(argumentName);
            }
        }

        /// <summary>
        /// Verifies that the int argument is not zero
        /// </summary>
        /// <param name="argumentValue">
        /// The integer to validate
        /// </param>
        /// <param name="argumentName">
        /// Name of the argument.
        /// </param>
        public static void VerifyIntArgument(int argumentValue, string argumentName)
        {
            if (argumentValue == 0)
            {
                throw new ArgumentException(argumentName);
            }
        }

        /// <summary>
        /// The replace ex.
        /// </summary>
        /// <param name="original">
        /// The original.
        /// </param>
        /// <param name="pattern">
        /// The pattern.
        /// </param>
        /// <param name="replacement">
        /// The replacement.
        /// </param>
        /// <returns>
        /// The replace ex.
        /// </returns>
        public static string ReplaceEx(string original, string pattern, string replacement)
        {
            int count, position0, position1;
            count = position0 = position1 = 0;
            string upperString = original.ToUpper();
            string upperPattern = pattern.ToUpper();
            int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length);
            var chars = new char[original.Length + Math.Max(0, inc)];
            while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1)
            {
                for (int i = position0; i < position1; ++i)
                {
                    chars[count++] = original[i];
                }

                for (int i = 0; i < replacement.Length; ++i)
                {
                    chars[count++] = replacement[i];
                }

                position0 = position1 + pattern.Length;
            }

            if (position0 == 0)
            {
                return original;
            }

            for (int i = position0; i < original.Length; ++i)
            {
                chars[count++] = original[i];
            }

            return new string(chars, 0, count);
        }

        /// <summary>
        /// Finds a given string and enclosing it with tags provided.
        /// </summary>
        /// <remarks>
        /// the search is Case Insensitive
        /// </remarks>
        /// <param name="sourceString">
        /// The source string.
        /// </param>
        /// <param name="findString">
        /// The find string.
        /// </param>
        /// <param name="startTag">
        /// The start tag.
        /// </param>
        /// <param name="endTag">
        /// The end tag.
        /// </param>
        /// <returns>
        /// The tag string.
        /// </returns>
        public static string TagString(string sourceString, string findString, string startTag, string endTag)
        {
            if (sourceString.IndexOf(findString, StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                return sourceString;
            }

            string newString;

            // If this is being called from a Web site, lets Decode 
            if (HttpContext.Current != null)
            {
                newString = HttpContext.Current.Server.HtmlDecode(sourceString);
            }
            else
            {
                newString = sourceString;
            }

            for (int i = 0; i < newString.Length; i++)
            {
                int pos = newString.IndexOf(findString, i, StringComparison.InvariantCultureIgnoreCase);

                // if she isnt found return original string
                if (pos == -1)
                {
                    return newString;
                }

                int newPos = pos + startTag.Length + findString.Length;
                newString = newString.Insert(pos, startTag);
                newString = newString.Insert(newPos, endTag);
                i = newPos + endTag.Length;
            }

            return sourceString;
        }

        #endregion [rgn]
    }
}